package com.yxym.homework.exercise;

import org.junit.Test;

import java.util.*;

@SuppressWarnings({"all"})
public class Homework01 {

    public static void main(String[] args) {
    }
    @Test
    public void test(){
        DOA<User> doa = new DOA<>();
        doa.save("01",new User(1,20,"xaihei"));
        doa.save("02",new User(2,25,"xaibai"));
        doa.save("03",new User(3,30,"xaiqing"));

        List<User> list = doa.list();
        System.out.println("list" + list);
        doa.delete("01");
        doa.update("02",new User(2,22,"小包"));
//        System.out.println("===============");
//        list = doa.list();
//        System.out.println(list);
    }
}

class DOA<T>{
    private Map<String ,T> map = new HashMap<>();
    public T get(String id){//方法中使用泛型
        return map.get(id);
    }

    public void update(String id, T entity){
        map.put(id,entity);
    }

    public List<T> list(){//方法中使用泛型
        //遍历map[k-v]，将map的所有value(entity)，封装到ArrayList返回即可
        List<T> list = new ArrayList<>();

        //遍历map
        Set<String> keySet = map.keySet();
//        Iterator iterator = keySet.iterator();
//        while (iterator.hasNext()) {
//            Object next =  iterator.next();
//            System.out.println(next);
//        }
        for (String o :keySet) {
            list.add(map.get(o));
        }
        return list;
    }

    public void delete(String id){
        map.remove(id);
    }
    public void save(String id,T entity){
        map.put(id,entity);
    }
}